package com.mayn.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class RegUtil {
	private static final Logger LOG = LoggerFactory.getLogger(RegUtil.class);

	public static List<String> GetRegList(String content, String regex, int group) {
		List<String> list = new ArrayList<String>();
		if (content == null || content.equals("")) {
			LOG.error("Content is null");
			return list;
		}
		if (regex == null || regex.equals("")) {
			LOG.error("regex is null");
			return list;
		}
		try {
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(content);
			String t = "";
			while (m.find()) {
				t = m.group(group).trim();
				list.add(t);
			}
		} catch (Exception e) {
			LOG.error("GetRegList() Exception", e);
		}
		return list;
	}

	public static String GetRegStr(String content, String regex, int group) {
		String ret = "";
		if (content == null || content.equals("")) {
			LOG.error("Content is null");
			return ret;
		}
		if (regex == null || regex.equals("")) {
			LOG.error("regex is null");
			return ret;
		}
		try {
			Pattern p;
			Matcher m;
			p = Pattern.compile(regex);
			m = p.matcher(content);
			if (m.find()) {
				ret = m.group(group).trim();
				return ret;
			}
		} catch (Exception e) {
			LOG.error("GetRegStr Exception", e);
		}
		return ret;
	}

	public static String RegRemoveStr(String content, String regex) {
		String ret = "";
		if (content == null || content.equals("")) {
			LOG.error("Content is null");
			return ret;
		}
		if (regex == null || regex.equals("")) {
			LOG.error("regex is null");
			return ret;
		}
		try {
			Pattern p = Pattern.compile(regex, 2);
			Matcher m = p.matcher(content);
			ret = m.replaceAll("");
		} catch (Exception e) {
			LOG.error("RegRemoveStr error");
		}
		return ret;
	}
	
	public static String delHTMLTag(String htmlStr){ 
        String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 
        String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 
        String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 
         
        Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
        Matcher m_script=p_script.matcher(htmlStr); 
        htmlStr=m_script.replaceAll(""); //过滤script标签 
         
        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
        Matcher m_style=p_style.matcher(htmlStr); 
        htmlStr=m_style.replaceAll(""); //过滤style标签 
         
        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
        Matcher m_html=p_html.matcher(htmlStr); 
        htmlStr=m_html.replaceAll(""); //过滤html标签 

        return htmlStr.trim(); //返回文本字符串 
    } 
	
}
